package cn.jyszn.wxb.utils;

import java.security.MessageDigest;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by yasser on 2016/8/9.
 */
public class StringUtils {



    /**
     * 检查字符串是否为空，如果有为空的则返还true，否则返还false
     * @param contents
     * @return
     */
    public static boolean isEmpty(String ... contents){
        for(String str : contents){
            if (str == null || "".equals(str)) {
                return true;
            }
            str.trim();
        }
        return false;
    }

    /**
     * 验证手机号是否有效
     * @param phone
     * @return 有效返还：true，否则返还：false
     */
    public static boolean validatePhone(String phone){
        Pattern pattern = Pattern.compile("^1[3,5,7,8][0-9]{9}$");
        Matcher matcher = pattern.matcher(phone);
        return matcher.matches();
    }

    /**
     * 验证邮箱地址是否有效
     * @param email
     * @return 有效返还true,否则返还false
     */
    public static boolean validateEmail(String email){
        String str="^([a-zA-Z0-9]*[-_]?[a-zA-Z0-9]+)*@([a-zA-Z0-9]*[-_]?[a-zA-Z0-9]+)+[\\.][A-Za-z]{2,3}([\\.][A-Za-z]{2})?$";
        Pattern p = Pattern.compile(str);
        Matcher m = p.matcher(email);
        return m.matches();
    }


    /**
     * 使用md5算法得到32位的字符串的md5摘要字符串
     * @param str
     * @return
     */
    public static String getMD5Str(String str){
        char hexDigits[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
        try {
            byte[] btInput = str.getBytes();
            // 获得MD5摘要算法的 MessageDigest 对象
            MessageDigest mdInst = MessageDigest.getInstance("MD5");
            // 使用指定的字节更新摘要
            mdInst.update(btInput);
            // 获得密文
            byte[] md = mdInst.digest();
            // 把密文转换成十六进制的字符串形式
            int j = md.length;
            char chars[] = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                chars[k++] = hexDigits[byte0 >>> 4 & 0xf];
                chars[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(chars);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void main(String[] args) {
        /**
         *
         * 测试同步一下代码
         */

        System.out.println("410425199005204050".substring(13));
    }
}
